Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use and respect the passfile connection parameter #1129

Merged
merged 2 commits into from
Jul 7, 2023

Conversation

keymon
Copy link
Contributor

@keymon keymon commented Jun 9, 2023

The postgres documentation[1] regarding the password file, states
that:

password file to use can be specified using the connection parameter
passfile or the environment variable PGPASSFILE.

The current implementation of lib/pq only respects the environment
variable PGPASSFILE. This is not correct, but also limiting, as
the PGPASSFILE is global and we might want to use different files
for different clients in the same program.

Fixing that is easy, by just checking the parameter passfile first,
and if not, pull the value from PGPASSFILE.

[1] https://www.postgresql.org/docs/current/libpq-pgpass.html

How to test

Start a postgres

docker run --name libpq-postgres \
    --net host \
    -e POSTGRES_DB=pqgotest \
    -e POSTGRES_USER=$(whoami) \
    -e POSTGRES_HOST_AUTH_METHOD="trust" \
    -d postgres

Run the test go test -run TestPgpass -v .

The postgres documentation[1] regarding the password file, states
that:

password file to use can be specified using the connection parameter
passfile or the environment variable PGPASSFILE.

The current implementation of lib/pq only respects the environment
variable PGPASSFILE. This is not correct, but also limiting, as
the PGPASSFILE is global and we might want to use different files
for different clients in the same program.

Fixing that is easy, by just checking the parameter passfile first,
and if not, pull the value from PGPASSFILE.

[1] https://www.postgresql.org/docs/current/libpq-pgpass.html
Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your contribution!

// cleanup
os.Remove(pgpassFile)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we still keep the cleanup step?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is in a defer next to where the file is created

conn.go Outdated
@@ -233,7 +233,11 @@ func (cn *conn) handlePgpass(o values) {
if _, ok := o["password"]; ok {
return
}
filename := os.Getenv("PGPASSFILE")
// Get passfile from the options, if empty, get it from envvar
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the correct place to handle the env var is here:

pq/conn.go

Line 2009 in 381d253

func parseEnviron(env []string) (out map[string]string) {

then the logic after parseEnviron is called will automatically take care of making the connection string parameter override the env var:

for k, v := range parseEnviron(os.Environ()) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you are right, it makes sense. I didn't see that I focused in that function. Pushed a new commit.

Now the connection only checks the parameter passfile, that
is populated by parseEnviron.

Refactored the test for this
@keymon
Copy link
Contributor Author

keymon commented Jun 28, 2023

thanks for your contribution!
Thank you for looking at it and the review!

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants